package demo03;


import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.io.FileUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.IOException;
import java.util.*;

;

/**
 * Created by hp on 2020/2/26.
 */
public class Test01 {
    static ArrayList<Classes> classe = new ArrayList<>();

    public static void main(String[] args) throws Exception {
        t1();
        t2();
        t3();
        t4();
        ArrayList<Students> students = t5();
        t6(students);
        t7();
    }

    public static void t1() throws Exception {
       SAXReader sr =new SAXReader();

        Document doc = sr.read("data.xml");

        Element rootElement = doc.getRootElement();

        List<Element> forward = rootElement.elements("forward");
        for (Element element : forward) {
            if (element.attributeValue("name").equals("java")) {
                List<Element> classes = element.elements("classes");
                for (Element aClass : classes) {
                    ArrayList<Students> students = new ArrayList<>();
                    List<Element> student = aClass.elements("student");
                    for (Element element1 : student) {
                        HashMap<String, Object> map = new HashMap<>();
                        Students student1 = new Students();
                        map.put("name", element1.attributeValue("name"));
                        map.put("sex", element1.attributeValue("sex"));
                        map.put("score", element1.attributeValue("score"));
                        BeanUtils.populate(student1, map);
                        students.add(student1);
                    }
                    Classes classes1 = new Classes(aClass.attributeValue("cname"));
                    classes1.setStudents(students);
                    classe.add(classes1);
                }
            }
        }
        System.out.println(classe);
    }

    public static void t2() {
        HashMap<Classes, Double> map = new HashMap<>();
        for (int i = 0; i < classe.size(); i++) {
            int sum = 0;
            for (Students student : classe.get(i).getStudents()) {
                sum += student.getScore();
            }
            map.put(classe.get(i), 1.0 * sum / classe.get(i).getStudents().size());
        }
        double max = 0.0;
        for (Classes classes : map.keySet()) {
            if (map.get(classes) > max) {
                max = map.get(classes);
            }
        }
        System.out.println("平均分最高的班级是：");
        for (Classes classes : map.keySet()) {
            if (map.get(classes) == max) {
                System.out.println(classes.getCname());
            }
        }
    }

    public static void t3() {
        int mannum = 0, womannum = 0;
        HashMap<String, Double> map = new HashMap<>();
        for (Classes classes : classe) {
            for (Students student : classes.getStudents()) {
                if (student.getSex().equals("男")) {
                    mannum++;
                } else {
                    womannum++;
                }
                if (map.containsKey(student.getSex())) {
                    Double d = map.get(student.getSex());
                    d += student.getScore();
                    map.put(student.getSex(), d);
                } else {
                    map.put(student.getSex(), 1.0 * student.getScore());
                }
            }
        }
        for (String s : map.keySet()) {
            if (s.equals("男")) {
                Double d = map.get(s);
                double v = d / mannum;
                map.put(s, v);
            } else {
                Double d = map.get(s);
                double v = d / womannum;
                map.put(s, v);
            }
        }
        double max = 0;
        for (String s : map.keySet()) {
            if (map.get(s) > max) {
                max = map.get(s);
            }
        }
        System.out.print("平均分最高的是：");
        for (String s : map.keySet()) {
            if (map.get(s) == max) {
                System.out.println(s);
            }
        }
    }

    public static void t4() {
        int nanmax = 0, nvmax = 0;
        for (Classes classes : classe) {
            for (Students student : classes.getStudents()) {
                if (student.getSex().equals("男")) {
                    if (student.getScore() > nanmax) {
                        nanmax = student.getScore();
                    }
                } else {
                    if (student.getScore() > nvmax) {
                        nvmax = student.getScore();
                    }
                }
            }
        }
        System.out.println("男女生最高分分别是：");
        for (Classes classes : classe) {
            for (Students student : classes.getStudents()) {
                if (student.getSex().equals("男")) {
                    if (student.getScore() == nanmax) {
                        System.out.println(student);
                    }
                } else {
                    if (student.getScore() == nvmax) {
                        System.out.println(student);
                    }
                }
            }
        }
    }

    public static ArrayList<Students> t5() {
        ArrayList<Students> student = new ArrayList<>();
        for (Classes classes : classe) {
            for (Students students : classes.getStudents()) {
                student.add(students);
            }
        }
        Collections.sort(student, new Comparator<Students>() {
            @Override
            public int compare(Students o1, Students o2) {
                return o2.getScore() - o1.getScore();
            }
        });
        System.out.println(student);
        return student;
    }

    public static void t6(ArrayList<Students> students) {
        ArrayList<Students> stu1 = new ArrayList<>();
        ArrayList<Students> stu2 = new ArrayList<>();
        ArrayList<Students> stu3 = new ArrayList<>();
        ArrayList<Students> stu4 = new ArrayList<>();
        for (int i = 0; i < students.size(); i++) {
            if (i < 3){
                stu1.add(students.get(i));
            }else if (i >= 3 &&i < 6){
                stu2.add(students.get(i));
            }else if (i >= 6 &&i < 8){
                stu3.add(students.get(i));
            }else {
                stu4.add(students.get(i));
            }
        }
        for (Classes classes : classe) {
            if (classes.getCname().equals("java1")){
                classes.setStudents(stu1);
            }else if (classes.getCname().equals("java2")){
                classes.setStudents(stu2);
            }else if (classes.getCname().equals("java3")){
                classes.setStudents(stu3);
            }else {
                classes.setStudents(stu4);
            }
        }
        System.out.println(classe);
    }

    public static void t7() throws IOException {
        for (Classes classes : classe) {
            for (Students students : classes.getStudents()) {
                FileUtils.write(new File(classes.getCname()+".txt"),students.getName() + "," + students.getSex() + "," + students.getScore() + "\r\n","utf-8",true);
            }
        }
    }
}
